package org.redis.cluster.container;

import org.redis.cluster.RedisCluster;
import org.redis.cluster.DefaultRedisCluster;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StartupContainer {
	
	protected final Logger logger=LoggerFactory.getLogger(getClass());

	public static void main(String[] args) {
		new StartupContainer();
	}
	
	public StartupContainer() {
		try {
			RedisCluster redisCluster=initRedisCluster();
			redisCluster.startup();
			
			Runtime.getRuntime().addShutdownHook(new Thread(()->{
				try {
					redisCluster.shutdown();
					System.exit(0);
				}
				catch(Throwable t) {
					if(logger.isErrorEnabled()) {
						logger.error("",t);
					}
					
					System.exit(1);
				}
			}));
			
			synchronized(this) {
				this.wait();
			}
		}
		catch(Throwable t) {
			if(logger.isErrorEnabled()) {
				logger.error("",t);
			}
			
			System.exit(1);
		}
	}
	
	protected RedisCluster initRedisCluster() {
		return new DefaultRedisCluster();
	}
}